home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Why does my program do this?
- Date: Mon, 01 Apr 96 23:01:48 GMT
- Organization: none
- Message-ID: <828399708snz@genesis.demon.co.uk>
- References: <4jnln2$95j@dfw-ixnews3.ix.netcom.com> <4jpe4s$db2@penage.cs.laurentian.ca>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: relay-4.mail.demon.net!post.demon.co.uk!genesis.demon.co.uk
-
- In article <4jpe4s$db2@penage.cs.laurentian.ca>
- ecchan@calum.uwaterloo.ca "Edward C. Chan" writes:
-
- >>char scores[STUDENT][5];
- >>
- >> for (i=0; i <= students-1; i=i+1)
- >> {
- >> for (j=0; j <= tests-1; j=j+1)
- >> {
- >> gets(&scores[i][j]);
- >> }
- >> }
- >>
-
- The problem is that gets() takes a pointer to the first character of an
- array of char and writes a string to it. Therefore subsequent gets()
- calls are overwriting the data read by earlier ones. You need an array of
- char to hold each score. Also never use gets() since you can't prevent it
- overwriting the end of the buffer if the input data line is too long.
-
- >user error.
- >
- >replace
- >
- > gets(&scores[i][j]);
- >
- >with
- >
- > gets(scores[i][j]);
-
- scores[i][j] is a char and gets() requires a char * argument so this is
- plainly wrong, it probably won't even compile (if you include stdio.h).
-
- >andeverything shall work fine. Actually, you are lucky that the programme
- >doesn't crash on you.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-